#include "ProjectileManager.h"
#include "ComponentManager.h"

CProjectileManager::CProjectileManager(void){}

CProjectileManager::~CProjectileManager(void)
{
	for(vector<CEntityProjectile*>::iterator i = m_vectorOfProjectileType.begin(); i!= m_vectorOfProjectileType.end(); i++)
	{
		delete (*i);
	}
}

void CProjectileManager::init(void)
{
	for(int i = 0; i<eNumberOfProjectile; i++)
	{
		CEntityProjectile* newProjectile = new CEntityProjectile(0, 0, CVector2i(0, 0), true, 0);
		m_vectorOfProjectileType.push_back(newProjectile);
	}
}

void CProjectileManager::createProjectileType(eProjectileType type, int indexTextureProjectile, int indexSpriteProjectile, 
		int speed, int healthPoint, int damagePoint)
{
	m_vectorOfProjectileType[type]->setIndexTexture(indexTextureProjectile);
	m_vectorOfProjectileType[type]->setIndexSprite(indexSpriteProjectile);
	m_vectorOfProjectileType[type]->setSpeed(speed);
	m_vectorOfProjectileType[type]->setHealthPoint(healthPoint);
	m_vectorOfProjectileType[type]->setDamagePoint(damagePoint);

}

bool CProjectileManager::createProjectiles(int numberOfEntities)
{
	for(int i=0; i<numberOfEntities; i++)
	{
		CEntityProjectile* newProjectile = CM.WORLD().createEntityProjectile(0, 0, CVector2i(0, 0), 10);
		m_vectorProjectile.push_back(newProjectile);
	}

	return true;
}

bool CProjectileManager::activeProjectile(eProjectileType projectileType, const CVector2i& position, bool toTheRight
										  , eSide side)
{
	CEntityProjectile* type = m_vectorOfProjectileType[projectileType];

	for(vector<CEntityProjectile*>::iterator i = m_vectorProjectile.begin(); i!=m_vectorProjectile.end(); i++)
	{
		if(!(*i)->getActive())
		{
			//copy the type
			*(*i)=*type;

			//set position
			(*i)->setPosition(position);

			//change speed if needed
			if(!toTheRight)
				(*i)->setSpeed((*i)->getSpeed()*-1);

			//set the projectile to active
			(*i)->setActive(true);

			//set side
			(*i)->setSide(side);
			return true;
		}
	}
	return false;
}
